/*
 * text_edit.c
 * This file is part of gURL
 *
 * Copyright (C) 2013, 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "text_edit.h"

#include <assert.h>
#include <glib/gi18n.h>
#include <stdbool.h>
#include <string.h>
#include "utils.h"

G_DEFINE_TYPE(TextEdit, text_edit, GTK_TYPE_BOX)

static void text_edit_class_init(TextEditClass* klass) {
    klass->signals[ENCODING_CHANGED] =
        g_signal_new("encoding-changed", TEXT_EDIT_TYPE, G_SIGNAL_RUN_FIRST,
                     0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_INT);
}

static void on_body_text_encoding_changed(GtkWidget* widget, gpointer p) {
    TextEdit* te = TEXT_EDIT(p);
    gint cur_enc = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
    assert(cur_enc >= 0 && cur_enc < (gint)ENCODINGS_COUNT);
    te->encoding = cur_enc;
    g_signal_emit(te, TEXT_EDIT_GET_CLASS(te)->signals[ENCODING_CHANGED], 0,
                  te->encoding);
}

static void text_edit_init(TextEdit* self) {
    GtkWidget* hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
    gtk_widget_show(hbox);
    gtk_widget_set_margin_left(hbox, 5);
    gtk_widget_set_margin_top(hbox, 5);
    gtk_container_add(GTK_CONTAINER(self), hbox);
    gtk_box_set_child_packing(GTK_BOX(self), hbox, false, false, 0, GTK_PACK_START);

    self->topbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
    gtk_widget_show(self->topbar);
    gtk_container_add(GTK_CONTAINER(hbox), self->topbar);

    GtkWidget* label = gtk_label_new(_("Encoding"));
    gtk_widget_show(label);
    g_object_set(G_OBJECT(label), "margin-left", 7, "margin-top", 3, NULL);
    gtk_container_add(GTK_CONTAINER(self->topbar), label);

    self->encoding_combo = gtk_combo_box_text_new();
    gtk_widget_show(self->encoding_combo);
    gtk_widget_set_margin_top(self->encoding_combo, 3);
    gtk_container_add(GTK_CONTAINER(self->topbar), self->encoding_combo);
    g_signal_connect(G_OBJECT(self->encoding_combo), "changed",
                     G_CALLBACK(on_body_text_encoding_changed), self);
    for(guint i = 0; i < ENCODINGS_COUNT; ++i)
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(self->encoding_combo),
                                  NULL, get_encoding_name(i));
    gtk_combo_box_set_active(GTK_COMBO_BOX(self->encoding_combo), 0);
    self->encoding = 0;

    self->textview = gtk_text_view_new();
    gtk_widget_show(self->textview);
    gtk_widget_set_hexpand(self->textview, true);
    gtk_widget_set_vexpand(self->textview, true);
    GtkWidget* sw = add_to_scrolled_window(self->textview);
    g_object_set(G_OBJECT(sw), "margin", 7, NULL);
    gtk_container_add(GTK_CONTAINER(self), sw);
}

void text_edit_set_editable(TextEdit* self, gboolean editable) {
    gtk_text_view_set_editable(GTK_TEXT_VIEW(self->textview), editable);
    gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(self->textview), editable);
}

GtkTextBuffer* text_edit_get_buffer(TextEdit* self) {
   return gtk_text_view_get_buffer(GTK_TEXT_VIEW(self->textview));
}

GtkBox* text_edit_get_topbox(TextEdit* self) {
   return GTK_BOX(self->topbar);
}

void text_edit_set_encoding(TextEdit* self, Encoding enc) {
    assert(enc >= 0 && enc < ENCODINGS_COUNT);
    gtk_combo_box_set_active(GTK_COMBO_BOX(self->encoding_combo), enc);
}

Encoding text_edit_get_encoding(TextEdit* self) {
   return self->encoding;
}

GtkWidget* text_edit_new() {
    return g_object_new(TEXT_EDIT_TYPE, "orientation", GTK_ORIENTATION_VERTICAL,
                        NULL);
}
